package com.ruoyi.starbucks.service;

import com.ruoyi.common.utils.http.HttpUtils;
import lombok.extern.slf4j.Slf4j;

import java.text.MessageFormat;
@Slf4j
public class SrmService2 {
    public static String userName = "api-CDhoZ9";

    public static String password = "abc880918";

    public static String projectName = "星巴克";

    public static String itemId = "1091";

    public static String token = "";

    public static void Login()
    {
        //{"success": 1, "message": "登录成功", "token": "8q74as45d4q5w7ed8q4d5sq4d8q"}
        String url = MessageFormat.format("http://app.cominterface.com:8999/api/v1/common/login?userName={0}&password={1}",userName,password);
        String json = HttpUtils.sendGet(url,null);
        System.out.println("登录,json="+json);
        //1|登录成功|cc76f9764f6511ebb8e5f04da27576f3
        if (json.startsWith("1"))
        {
            token = json.split("\\|")[2];
            System.out.println("登录,token="+token);
        }
    }

    public static String GetPhoneNo() {
        if (token==null || token.isEmpty())
        {
            Login();
        }
        //{"success": 1, "message": "获取号码成功","phone": 15865695478, "operator": "移动", "province": "河南"}
        //1|获取号码成功|15865695478|移动|河南
        String url = MessageFormat.format("http://app.cominterface.com:8999/api/v1/common/getPhone?token={0}&itemId={1}",token,itemId);
        String json = HttpUtils.sendGet(url,null);
        if (json.startsWith("1"))
        {
            String data = json.split("\\|")[2];
            System.out.println("获取到的手机号:"+data);
            return data;
        }
        return "";
    }

    public static String GetMessage(String phoneNo) {
        if (token==null || token.isEmpty())
        {
            Login();
        }
        System.out.println("token="+token);
        //{"success": 1, "message": "获取短信成功","sms_content": "【三友食品】您的验证码是548562，三分钟内有效", "sms_code": "548562", "release": 1}
        //1|获取短信成功|【三友食品】您的验证码是548562，三分钟内有效|548562
        String url = MessageFormat.format("http://app.cominterface.com:8999/api/v1/common/getMessage?token={0}&itemId={1}&phone={2}",token,itemId,phoneNo);
        String json = HttpUtils.sendGet(url,null);
        if (json.startsWith("1"))
        {
            String data = json.split("\\|")[2];
            System.out.println("获取到的短信:"+data);
            return data;
        }
        return "ERROR:尚未收到短信，请确保您已经向手机号{phoneNo}发送过与项目【{projectName}】相关的短信";
    }

    public static String AddToBlackList(String phoneNo){
        if (token==null || token.isEmpty())
        {
            Login();
        }

        String url =MessageFormat.format("http://app.cominterface.com:8999/api/v1/common/addBlackList?token={0}&itemId={1}&phone={2}",token,itemId,phoneNo);
        String json = HttpUtils.sendGet(url,null);
        if (json.startsWith("1"))
        {
            String data = json.split("\\|")[1];
            System.out.println("加入黑名单:"+data);
            return data;
        }
        return "ERROR:号码拉黑失败,请再试一次";
    }

    public static void main(String[] args) {
        /**
        *登录
        */
//        Login();
        /**
         * 获取手机号码
         */
//        GetPhoneNo();
        /**
         * 获取短信
         */
//        GetMessage(GetPhoneNo());
        /**
         * 加入黑名单
         */
//        AddToBlackList(GetPhoneNo());
    }
}
